package com.example.addressbook;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.content.Intent; 
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import com.bumptech.glide.Glide;
import android.support.design.widget.TextInputLayout;
import org.litepal.crud.DataSupport;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.content.Context;
import com.bumptech.glide.Glide;
import org.litepal.crud.DataSupport;
import android.widget.PopupMenu;
import android.app.Dialog;
import android.view.MenuItem;
import android.view.Gravity; 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;

import java.util.*;

public class ModifyActivity extends AppCompatActivity {
	
	public static int [] pictures ={
		R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,
		R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,
		R.drawable.p11,R.drawable.p12,R.drawable.p13,R.drawable.p14,R.drawable.p15,
		R.drawable.p16,R.drawable.p17,R.drawable.p18,R.drawable.p19,R.drawable.p20,
	};
	
	ImageView imageView;
	TextInputLayout nameTextInput;
	
	@Override
	public void onCreate(Bundle savedIntanceState){
		super.onCreate(savedIntanceState);
		setContentView(R.layout.modify_activity);
		android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar_modify);
		toolbar.setTitle("编辑联系人");
		setSupportActionBar(toolbar);
		
		imageView = (ImageView)findViewById(R.id.mod_img);
		final int random = (int)(Math.random()*20); 
		//imageView.setImageResource(pictures[random]);
		Glide.with(this).load(pictures[random]).into(imageView);
		imageView.setOnClickListener( new View.OnClickListener(){
			@Override
			public void onClick(View v){
				AlertDialog dialog = createDialog();
				dialog.show();
				//Toast.makeText(ModifyActivity.this,"成功",Toast.LENGTH_SHORT).show();
			}
		});
		nameTextInput = (TextInputLayout)findViewById(R.id.name_tl);
		Intent intent = getIntent();
		String s = intent.getStringExtra("name");
		if( s!=null ) nameTextInput.getEditText().setText(s);
		Button loginButton = (Button)findViewById(R.id.login_button);
		loginButton.setOnClickListener( new View.OnClickListener(){
			@Override
			public void onClick(View v){
				TextInputLayout telTextInput = (TextInputLayout)findViewById(R.id.tel_text_input_layout);
				String tel = telTextInput.getEditText().getText().toString();
				if( tel.length() != 11 ){
					telTextInput.setErrorEnabled(true);
					telTextInput.setError("电话号码格式不正确");
				}else{
					telTextInput.setErrorEnabled(false);
					
					TextInputLayout infoTextInput = (TextInputLayout)findViewById(R.id.info_tl);
					String name = nameTextInput.getEditText().getText().toString();
					String info = infoTextInput.getEditText().getText().toString();
					int temp = pictures[random];
					if( pictureId != -1 ) temp = pictureId;
					People people = new People(name,tel,info,temp);
					List<People> list = DataSupport.where("name = ?",people.getName()).find(People.class);
					if( list.size() == 0 ){
						people.save();		// database
						Toast.makeText(ModifyActivity.this,"编辑联系人成功",Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						setResult(RESULT_OK,intent);
						finish();
					}else{
						People ppp = new People();
						ppp.setTelephone(tel);
						ppp.setInformation(info);
						ppp.setPicturePath(temp);
						ppp.updateAll("name = ?",name);
						
						Friend fff = new Friend();
						fff.setTelephone(tel);
						fff.setInformation(info);
						fff.setPicturePath(temp);
						fff.updateAll("name = ?",name);
						
						Toast.makeText(ModifyActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
						finish();
					} 
				}
			}	
		});
	}
	
	public int pictureId = -1;
	
	@Override
	public void onResume(){
		pictureId = SelectAdapter.getPictureId();
		//Toast.makeText(ModifyActivity.this,"select_activity_onResume",Toast.LENGTH_SHORT).show();
		super.onResume();
		if( pictureId != -1 ){
			Glide.with(this).load(pictureId).into(imageView);
		}
	}
	
	public AlertDialog createDialog(){
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		View view = LayoutInflater.from(this).inflate(R.layout.alert_dialog,null);
		dialog.setView(view);

		AlertDialog d = dialog.create(); 
		RecyclerView recyclerView = view.findViewById(R.id.select_recycler_view);
		SelectAdapter selectAdapter = new SelectAdapter(this,d,imageView);
		LinearLayoutManager layoutManager = new LinearLayoutManager(this);
		layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.setAdapter(selectAdapter);
		
		return d;
	}
	
}